/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';

var BaseFilter = require('./FilterBase.js');
var FilterStop = require('./FilterStop.js');
var FilterAdv = require('./FilterAdv.js');
var FilterLink = require('./FilterLink.js');
var FilterNumber = require('./FilterNumber.js');
var FilterPrep = require('./FilterPrep.js');
var FilterQuantifier = require('./FilterQuantifier.js');
var FilterSingle = require('./FilterSingle.js');
var FilterSenseless = require('./FilterSenseless.js');
var FilterWeightless = require('./FilterWeightless.js');

var __isDebug = true;
/**
 * 词语过滤链
 * @param isDebug {boolean} 是否debug模式
 * @constructor
 */
function FilterChain(isDebug) {
  __isDebug = isDebug || false;
  this.__layer = [];
}

module.exports = FilterChain;

FilterChain.prototype.addFilter = function (aFilter) {
  if (!aFilter instanceof BaseFilter) {
    throw new Error('addFilter args must instanceof BaseFilter');
  }
  this.__layer.push(aFilter);
};

FilterChain.prototype.doFilte = function (srcText) {
  for (var aFilter of this.__layer) {
    if (!aFilter.filter(srcText)) {
      return false;
    }
  }
  return true;
};

FilterChain.prototype.showAndClean = function (filterName) {
  if (!__isDebug) {
    console.log('FilterChain not in debug mode');
    return;
  }

  if (filterName !== undefined && typeof filterName === "number") {
    //console.log('number');
    var aFilter = this.__layer[filterName];
    if (aFilter) aFilter.showAndClean();
    else console.log('max is ' + this.__layer.length - 1);
  } else if (filterName !== undefined) {
    //console.log('filterName');
    for (var aFilter of this.__layer) {
      if (aFilter.constructor.name === filterName) {
        aFilter.showAndClean();
      }
    }
  } else {
    //console.log('all');
    for (var aFilter of this.__layer) {
      aFilter.showAndClean();
    }
  }
};

/**
 * @param isDebug isDebug mode 注意,chain是有顺序关系的
 * @returns {FilterChain}
 */
FilterChain.defaultFilterChain = function (isDebug) {
  var aFilterChain = new FilterChain(isDebug || false);

  // add filter FilterStop
  aFilterChain.addFilter(new FilterStop(__isDebug));
  aFilterChain.addFilter(new FilterSingle(__isDebug));
  aFilterChain.addFilter(new FilterNumber(__isDebug));
  aFilterChain.addFilter(new FilterQuantifier(__isDebug));
  aFilterChain.addFilter(new FilterLink(__isDebug));
  aFilterChain.addFilter(new FilterAdv(__isDebug));
  aFilterChain.addFilter(new FilterPrep(__isDebug));
  aFilterChain.addFilter(new FilterSenseless(__isDebug));
  aFilterChain.addFilter(new FilterWeightless(__isDebug));
  return aFilterChain;
};
